package Envo 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	
	/**
	 * @author Sam Morrison
	 */
	public class Game extends MovieClip
	{
		// Layers
		public var playerLayer:Sprite;				// Player
		public var levelLayer:Sprite;				// Level
		
		// Objects
		public var player:Player;					// Player
		public var level:Level;						// Level
		public var physics:Physics;					// Physics
		public var animation:Animation;				// Animation
		
		// XML
		public var xml:XML;							// XML
		
		public function Game() 
		{
			
		}
		
		public function update(e:Event):void
		{
			player.update();
			
			physics.update(player, level);
			
			level.update(player);
			
			animation.update(player);
			
			player.previousX = player.x;
			player.previousY = player.y;
		}
		
		public function start():void
		{
			// Instantiate Layers
			playerLayer = new Sprite();
			levelLayer = new Sprite();
			
			// Set Up Layers
			addChild(playerLayer);
			addChild(levelLayer);
			
			// Create a player
			player = new Player();
			
			// Add player to layer
			playerLayer.addChild(player);
			
			physics = new Physics(this);
			
			animation = new Animation();
			
			// Read XML maps
			var xmlRequest:URLRequest = new URLRequest("maps.xml");
			var xmlLoader:URLLoader = new URLLoader(xmlRequest);
			xmlLoader.addEventListener(Event.COMPLETE, onXMLLoaded);
		}
		
		public function onXMLLoaded(e:Event):void
		{
			xml = new XML(e.target.data);
			level = new Level(xml, this);
			levelLayer.addChild(level);
			level.load();
			addEventListener(Event.ENTER_FRAME, update);
		}
		
	}

}